const { Controller } = require('egg');

class FileController extends Controller {
    async list() {

        const { ctx } = this;
        const currentUser = ctx.authUser;
        const ext = ctx.request.body.ext
        const Op = this.app.Sequelize.Op
        const res = await this.app.model.File.findAll({
            where: {
                name: {
                    [Op.like]: `%${ctx.request.body.name}%`

                },
                ext: ext,
                user_id: currentUser.id
            }
        })
        console.log(res, "res")

        ctx.apiSuccess(res, "", 200);
    }
    async updatefilename() {
        const currentUser = this.ctx.authUser;
        this.ctx.validate({
            fileName: {
                type: "string",
                required: true,
                defValue: "",
                desc: "文件名称",
            },
            id: {
                type: "string",
                required: true,
                defValue: "",
                desc: "id",

            }
        })
        const file = await this.app.model.File.findOne({
            where: {
                user_id: currentUser.id,
                id: this.ctx.request.body.id,

            }
        })
        if (!file) {
            return this.ctx.throw(400, "该文件不存在")
        }
        file.name = this.ctx.request.body.fileName
        const res = await file.save()
        this.ctx.apiSuccess(res, "修改成功", 200)
    }
    async delete(){
        const user_id=this.ctx.authUser.id
        this.ctx.validate({
            id:{
                required:true,
                type:"string",
                desc:"记录"
            }
        })
        let files=await this.app.model.File.findOne({
            where:{
                id:this.ctx.request.body.id,
                user_id
            }
        })
        let size=0;
             size+=files.size
 
        let res=await this.app.model.File.destroy({
            where:{
                id:this.ctx.request.body.id,
                user_id
            }
        })
        if(res){
            size=this.ctx.authUser.used_size-size
            this.ctx.authUser.used_size=size>0?size:0;
            this.ctx.authUser.save()
        }
        this.ctx.apiSuccess(res,"删除成功",200)
    }
    async pldelete(){
        const user_id=this.ctx.authUser.id
        this.ctx.validate({
            ids:{
                required:true,
                type:"string",
                desc:"记录"
            }
        })
        let files=await this.app.model.File.findAll({
            where:{
                id:this.ctx.request.body.ids.split(","),
                user_id
            }
        })
        let size=0;
        files.forEach(el => {
            size+=el.size
        });
        let res=await this.app.model.File.destroy({
            where:{
                id:this.ctx.request.body.ids.split(","),
                user_id
            }
        })
        if(res){
            size=this.ctx.authUser.used_size-size
            this.ctx.authUser.used_size=size>0?size:0;
            this.ctx.authUser.save()
        }
        this.ctx.apiSuccess(res,"批量删除成功",200)
    }
}

module.exports = FileController;
